package com.retlife.tvshow.ui

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView.Adapter
import androidx.recyclerview.widget.RecyclerView.ViewHolder
import com.retlife.tvshow.R
import com.retlife.tvshow.global.RunningContext

class BikeDataAdapter : Adapter<BikeDataAdapter.BikeDataViewHolder>() {

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BikeDataViewHolder {
        val inflate = LayoutInflater.from(parent.context).inflate(R.layout.layout_bike_info, null)
        inflate.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        return BikeDataViewHolder(inflate)
    }

    override fun onBindViewHolder(holder: BikeDataViewHolder, position: Int) {
        val bikeData = RunningContext.bikeDataList[position]
        holder.tvIndex.text = "${position + 1}"
        holder.tvTorque.text = bikeData.torqueAverage.toString()
        holder.tvSpeed.text = bikeData.speed.toString()
        if (bikeData.isActive) {
            holder.layout.setBackgroundResource(R.mipmap.bg_list_item)
        } else {
            holder.layout.setBackgroundResource(R.mipmap.bg_list_item1)
        }
    }

    override fun getItemCount(): Int {
        return RunningContext.bikeDataList.size
    }


    class BikeDataViewHolder(itemView: View) : ViewHolder(itemView) {
        val tvIndex: TextView
        val tvSpeed: TextView
        val tvTorque: TextView
        val layout: LinearLayout

        init {
            tvIndex = itemView.findViewById(R.id.tv_index)
            tvSpeed = itemView.findViewById(R.id.tv_rotate_speed)
            tvTorque = itemView.findViewById(R.id.tv_torque)
            layout = itemView.findViewById(R.id.layout)
        }
    }
}